﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Application.MainModule.BillingInformationManagement;
using Domain.MainModule.Entities;
using Presentation.Web.MVC.Client.ViewModels;
using System.Web.Routing;

namespace Presentation.Web.MVC.Client.Controllers
{
    public class BillingInformationController : Controller
    {
        #region private member

        /// <summary>
        /// Instance of the billing information service.
        /// </summary>
        private IBillingInformationManagementService _billingInformationService;

        #endregion

        #region constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="BillingInformationController"/> class.
        /// </summary>
        /// <param name="billingInformationService">The billing information service.</param>
        public BillingInformationController(IBillingInformationManagementService billingInformationService)
        {
            _billingInformationService = billingInformationService;
        }

        #endregion

        /// <summary>
        /// Display a paged listing of the existing billing informations.
        /// </summary>
        /// <param name="page">The page.</param>
        /// <param name="pageSize">The page size.</param>
        /// <returns></returns>
        public ActionResult Index(int page, int pageSize)
        {
            IList<BillingInformation> billingInformations = _billingInformationService.FindPagedBillingInformation(page, pageSize);
            return View(new BillingInformationListViewModel(billingInformations, page, pageSize));
        }

        /// <summary>
        /// Shows a specific billing information in detail.
        /// </summary>
        /// <param name="billingInformationId">The billing information id.</param>
        /// <returns>A view of the billing information details.</returns>
        public ActionResult Details(Guid billingInformationId)
        {
            //Get the billing information aggregate.
            BillingInformation billingInformation = _billingInformationService.FindBillingInformationByIdSpecification(billingInformationId);
            //Display its details.
            return View(billingInformation);
        }

        /// <summary>
        /// Displays a form to create a new billing information.
        /// </summary>
        /// <returns></returns>
        public ActionResult Create()
        {
            return View();
        }

        /// <summary>
        /// Creates the specified billing information based on the request data and displays it.
        /// </summary>
        /// <param name="customer">The billing information to add.</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Create(BillingInformation billingInformation)
        {
            //Check if there aren't validation errors on the customer.
            if (ModelState.IsValid)
            {
                //Add billing information to the data base.
                _billingInformationService.AddBillingInformation(billingInformation);
                //Show the first page of the billing information.
                return RedirectToAction("Index", new { page = 0, pageSize = 10 });
            }
            else
            {
                //Return the Create view and display the validation errors.
                return View();
            }
        }

        /// <summary>
        /// Displays a form to edit the billing information.
        /// </summary>
        /// <param name="billingInformationId">The billing information code.</param>
        /// <returns></returns>
        public ActionResult Edit(Guid billingInformationId)
        {
            //Get the billing information aggregate to edit.
            BillingInformation billingInformation = _billingInformationService.FindBillingInformationByIdSpecification(billingInformationId);
            //Return the
            return View(billingInformation);
        }

        /// <summary>
        /// Saves the changes on the editions performed to the specified billing information.
        /// </summary>
        /// <param name="billingInformation">The billing information.</param>
        /// <returns>The details of the billing information.</returns>
        [HttpPost]
        public ActionResult Edit(BillingInformation billingInformation)
        {
            if (ModelState.IsValid)
            {
                _billingInformationService.ChangeBillingInformation(billingInformation);
                return RedirectToAction("Details", new RouteValueDictionary() { { "billingInformationId", billingInformation.BillingInformationId } });
            }
            else
            {
                return View();
            }
        }

        /// <summary>
        /// Deletes the specified billing information.
        /// </summary>
        /// <param name="billingInformationId">The billing information code.</param>
        /// <returns>The details of the billing information.</returns>
        public ActionResult Delete(Guid billingInformationId)
        {
            BillingInformation billingInformation = _billingInformationService.FindBillingInformationByIdSpecification(billingInformationId);
            _billingInformationService.RemoveBillingInformation(billingInformation);
            
            return RedirectToAction("Index", new RouteValueDictionary() { { "page", 0 }, { "pageSize", 10 } });
        }
    }
}
